﻿/* *********************************************
 * Crypto libary : PasswordGenerator module
 * Christiaan Rakowski 20110825
 * This module contains a simple password generator
 * ********************************************/

using System;

namespace CryptoCs
{
	namespace PasswordUtils
	{
		public enum CharSets
		{
			AlphaLowerOnly = 0,
			Alpha,
			NumbersOnly,
			AlphaNumeric,
			AlphaNumericSpecial
		};

		public class PasswordGenerator
		{
			private static readonly string Lower = "abcdefghijklmnopqrstuvwxyz";
			private static readonly string Upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
			private static readonly string Numbers = "0123456789";
			private static readonly string Special = "!@#$%^&*()-_=+[]{}|;:,.<>?";

			public static string GeneratePassword(int length, CharSets charset)
			{
				string password = "";
				string chars = "";

				#region Fill chars with the charset

				switch (charset)
				{
					case CharSets.AlphaLowerOnly:
						{
							chars = Lower;
							break;
						}
					case CharSets.Alpha:
						{
							chars = Lower + Upper;
							break;
						}
					case CharSets.NumbersOnly:
						{
							chars = Numbers;
							break;
						}
					case CharSets.AlphaNumeric:
						{
							chars = Lower + Upper + Numbers;
							break;
						}
					case CharSets.AlphaNumericSpecial:
						{
							chars = Lower + Upper + Numbers + Special;
							break;
						}
					default:
						{
							return null;
						}
				}

				#endregion

				Random rand = new Random();

				for (int i = 0; i < length; i++)
				{
					password += chars[rand.Next(chars.Length)];
				}

				return password;
			}
		}
	}
}
